package edu.uwosh.cs342.group1.storage;

import edu.uwosh.cs342.group1.database.StringEncryptor;

public class Scores {
	private String username;
	private String score;
	private Quiz quiz;
	private int isPublic;
	
	public Scores() {
		this.quiz = new Quiz();
	}
	
	/**
	 * Gets the username of the quiz score
	 * @return
	 */
	public String getUsername() {
		return this.username;
	}
	
	/**
	 * Sets the username of the user for this quiz score
	 * @param username
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	
	/**
	 * Gets the score as ("correct/total percent%")
	 * @return
	 */
	public String getScore() {
		return this.score;
	}
	
	/**
	 * Sets the score string
	 * @param score
	 */
	public void setScore(String score) {
		this.score = score;
	}
	
	/**
	 * Encrypts the score string
	 * @param score
	 * @return
	 */
	public static String EncryptScore(String score) {
		return StringEncryptor.Encrypt(score);
	}
	
	/**
	 * Decrypts the score from its encrypted form
	 * @param score
	 * @return
	 */
	public static String DecryptScore(String score) {
		return StringEncryptor.Decrypt(score);
	}
	
	/**
	 * Calculates the score and returns a string as correct/total percent%
	 * The percent is calculated to nn.mm hundredths of a decimal
	 * @param correct
	 * @param total
	 * @return
	 */
	public static String calculateScore(int correct, int total) {
		Float result = ((float)correct/total) * 100;
		String resultStr = result.toString();
		return correct + "/" + total + " " + resultStr.substring(0, resultStr.indexOf(".") + 2) + "%";
	}
	
	/**
	 * Gets the quiz information for the score
	 * Needed is the id and the name
	 * @return
	 */
	public Quiz getQuiz() {
		return this.quiz;
	}
	
	/**
	 * Sets the quiz information for the score
	 * @param quiz
	 * @return
	 */
	public void setQuiz(Quiz quiz) {
		this.quiz.setId(quiz.getId());
		this.quiz.setName(quiz.getName());
	}
	
	/**
	 * Gets the flag to show the scores online
	 * @return
	 */
	public int getIsPublic() {
		return this.isPublic;
	}
	
	/**
	 * Sets the flag to show the scores online
	 * @param i
	 */
	public void setIsPublic(int i){
		this.isPublic=i;
	}
	
	/**
	 * Shows the score
	 */
	@Override
	public String toString() {
		return this.username + " : " + this.quiz.getName() + " : " + this.score;
	}
}
